<?php
/**
 * Created by PhpStorm.
 * User: 共赢岛
 * Date: 2024/6/21
 * Time: 17:21
 */

namespace App\Admin\Services;


use App\Models\PositionMonthTarget;
use App\Models\PositionMonthTargetItem;
use App\Models\User;
use App\Models\UserMonthAwardPunish;
use App\Models\UserMonthTarget;

class UserMonthTargetService
{
    public static function isTarget($param = [])
    {
        $id = $param['id'];
        unset($param['id']);

        $target = UserMonthTarget::where($param)->first();

        if ($target) {
            if ($id == $target->id) {
                return false;
            } else {
                return true;
            }
        }

        return false;
    }

    public static function itemCreate($param = [])
    {
        $condition['position_month_target_id'] = $param['position_month_target_id'];
        $condition['vin_code'] = $param['VIN'];
        $condition['score'] = $param['积分抵扣金额'];
        $condition['items'] = json_encode($param);

        PositionMonthTargetItem::create($condition);

        return true;
    }

    public static function itemSave($param = [])
    {
        $item = PositionMonthTargetItem::where('vin_code', $param['vin_code'])
            ->where('position_month_target_id', $param['position_month_target_id'])
            ->first();

        if ($item) {
            $user = User::where('name', $param['user_name'])->first();

            if ($user) {
                $item->user_name = $param['user_name'];
                $item->user_id = $user->id;

                $item->save();
            }
        }
    }

    public static function targetCreate($param = [])
    {
        $user = User::where('name', $param['姓名'])->first();

        if ($user) {
            $target = UserMonthTarget::where('position_month_target_id', $param['position_month_target_id'])
                ->where('user_id', $user->id)
                ->first();

            if ($target) {
                $target->output_value_target = $param['产值目标'];
                $target->maintenance_vehicle_target = $param['定保台次目标'];
                $target->meet_car_commission_coefficient = $param['接车提成系数'] ?? $target->meet_car_commission_coefficient;
                $target->output_value_commission_coefficient = $param['产值提成系数'] ?? $target->output_value_commission_coefficient;

                $target->save();
            } else {
                $position_target = PositionMonthTarget::find($param['position_month_target_id']);

                UserMonthTarget::create([
                    'position_month_target_id' => $param['position_month_target_id'],
                    'user_id' => $user->id,
                    'target_date' => $position_target->target_date,
                    'output_value_target' => $param['产值目标'],
                    'maintenance_vehicle_target' => $param['定保台次目标'],
                    'meet_car_commission_coefficient' => $param['接车提成系数'],
                    'output_value_commission_coefficient' => $param['产值提成系数']
                ]);
            }
        }
    }

    public static function awardPunishCreate($param = [])
    {
        $user = User::where('name', $param['姓名'])->first();

        if ($user) {
            $position_target = PositionMonthTarget::find($param['position_month_target_id']);

            UserMonthAwardPunish::create([
                'position_month_target_id' => $param['position_month_target_id'],
                'user_id' => $user->id,
                'target_date' => $position_target->target_date,
                'title' => $param['属性名称'],
                'price' => $param['金额'],
                'type' => $param['类型'] == '奖励' ? DEFAULT_TYPE_ONE : DEFAULT_TYPE_TWO,
            ]);
        }
    }

    public static function realOutputValue($row)
    {
        $row->real_output_value = ErpRepairSettlementService::erpRealOutputValue([
            'user_erp_id' => $row->user->erp_id,
            'target_date' => $row->target_date,
            'store_erp_id' => $row->user->store->erp_id
        ]);

        $score = PositionMonthTargetItem::where('position_month_target_id', $row->position_month_target_id)
            ->where('user_id', $row->user_id)
            ->sum('score');

        return ($row->real_output_value + $score)/10000;
    }

    public static function outputValueAchievementCommission($row)
    {
        return sprintf("%.2f",($row->real_output_value*10000) * ($row->output_value_commission_coefficient/100));
    }

    public static function outputValueReach($row)
    {
        return ((($row->real_output_value*10000) / $row->output_value_target) / 100);
    }

    public static function outputValueScore($row)
    {
        if ($row->output_value_reach >= 100) {
            $row->output_value_score = '0.4';
        } elseif ($row->output_value_reach >= 90 && $row->output_value_reach <= 100) {
            $row->output_value_score = '0.35';
        } else {
            $row->output_value_score = '0.3';
        }

        return $row->output_value_score;
    }

    public static function maintenanceVehicleReach($row) {
        return ($row->real_maintenance_vehicle / $row->maintenance_vehicle_target) * 100;
    }

    public static function maintenanceVehicleScore($row) {
        if ($row->maintenance_vehicle_reach >= 100) {
            $row->maintenance_vehicle_score = '0.7';
        } elseif ($row->maintenance_vehicle_reach >= 90 && $row->maintenance_vehicle_reach <= 100) {
            $row->maintenance_vehicle_score = '0.5';
        } else {
            $row->maintenance_vehicle_score = '0.4';
        }

        return $row->maintenance_vehicle_score;
    }

    public static function userMonthAward($row) {
        $row->user_month_award = UserMonthAwardPunish::where('position_month_target_id', $row->position_month_target_id)
            ->where('user_id', $row->user_id)
            ->where('type', DEFAULT_TYPE_ONE)
            ->sum('price');

        return $row->user_month_award;
    }

    public static function userMonthPunish($row) {
        $row->user_month_punish = UserMonthAwardPunish::where('position_month_target_id', $row->position_month_target_id)
            ->where('user_id', $row->user_id)
            ->where('type', DEFAULT_TYPE_TWO)
            ->sum('price');

        return $row->user_month_punish;
    }

    public static function subtotalMeritPay($row) {
        return ($row->erp_meet_car_commission + $row->output_value_achievement_commission) * $row->kpi_coefficient + $row->user_month_award - $row->user_month_punish;
    }

    public static function shouldMeritPay($row) {
        return sprintf("%.2f",$row->subtotal_merit_pay * ($row->gross_profit_reach / 100));
    }
}